package Service.Admin.Imp;

import Dao.Admin.AccountUpdateDao;
import Dao.DaoFactory;
import Dao.Role.StuInfoDao;
import Dao.Role.AllStudentDao;
import Dao.Role.AllTeacherDao;
import Dao.Role.TeacherDao;
import Entity.MyTableData;
import Entity.Role.Student;
import Entity.Role.Teacher;
import Service.Admin.AccountService;
import Util.DBUtil;

import java.sql.Connection;
import java.util.List;

public class AccountServiceImp implements AccountService {
    private AllStudentDao allStudentDao;
    private AllTeacherDao allTeacherDao;
    private AccountUpdateDao accountUpdateDao;
    private Connection conn;

    public AccountServiceImp() {
        conn = DBUtil.getConnByDruid();
        allStudentDao = DaoFactory.getInstance("allStudentDao");
        allTeacherDao = DaoFactory.getInstance("allTeacherDao");
        accountUpdateDao = DaoFactory.getInstance("accountUpdateDao");
    }

    /**
     * 查询所有学生信息
     */
    @Override
    public MyTableData getAllStudent() {
        List<Student> stus = allStudentDao.getAllStu(conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", stus.size(), stus);
    }


    /**
     * 查询所有教师信息
     */
    @Override
    public MyTableData getAllTeacher() {
        List<Teacher> teas = allTeacherDao.getAllTea(conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", teas.size(), teas);
    }


    /**
     * 新增学生信息
     */
    public Boolean studentAdd(Student student) {
        boolean flag = accountUpdateDao.studentAdd(student, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }

    /**
     * 通过学号编辑学生信息
     */
    public Boolean studentUpdate(Student student, String sno) {
        boolean flag = accountUpdateDao.studentUpdate(student, sno, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }

    /**
     * 通过学号删除学生信息
     */
    public Boolean studentDelete(String sno) {
        boolean flag = accountUpdateDao.studentDelete(sno, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }

    /**
     * 新增教师信息
     */
    public Boolean teacherAdd(Teacher teacher) {
        boolean flag = accountUpdateDao.teacherAdd(teacher, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }

    /**
     * 通过工号编辑教师信息
     */
    public Boolean teacherUpdate(Teacher teacher, String tno) {
        boolean flag = accountUpdateDao.teacherUpdate(teacher, tno, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }

    /**
     * 通过工号删除教师信息
     */
    public Boolean teacherDelete(String tno) {
        boolean flag = accountUpdateDao.teacherDelete(tno, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }


    /**
     * 新增公告信息
     */
    public Boolean messageAdd(String msg_title, String msg_from, String msg_content) {
        boolean flag = accountUpdateDao.messageAdd(msg_title, msg_from, msg_content, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }


}
